﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace Jy123.Web.Manage
{
    public partial class Register : MgConfig
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Verify("Register.aspx");
            if (!IsPostBack)
            {
                if (Request.QueryString["msg"] != null)
                {
                    if (Request.QueryString["msg"] == "ok")
                    {
                        Message = MessageBox("添加管理员成功！", MsgType.ok);
                    }
                }

                dropRight.DataSource = new BLL.Manage_Right().GetList("");
                dropRight.DataTextField = "RName";
                dropRight.DataValueField = "ID";
                dropRight.DataBind();
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                new BLL.Manage_Main().Add(new Model.Manage_Main()
                {
                    MName = txtName.Value,
                    Password = DBUtility.DESEncrypt.Md5(pwd.Value, true),
                    Email = txtEmail.Value,
                    FMRight = Convert.ToInt32(dropRight.SelectedItem.Value),
                    LastLogin = DateTime.Now,
                    QuickMenu = string.Empty,
                    LoginIP = string.Empty
                });
                Response.Redirect(Request.Url.ToString() + "?msg=ok");
            }
            catch
            {
                Message = MessageBox("操作错误！", MsgType.error);
            }
        }
        protected string GetRightName(object rid)
        {
            Model.Manage_Right mright = new BLL.Manage_Right().GetModel(int.Parse(rid.ToString()));
            return mright.RName;
        }
        protected DataSet GetRightList()
        {
            return new BLL.Manage_Right().GetList(string.Empty);
        }
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                HiddenField hidRight = (HiddenField)e.Row.FindControl("hidRight");
                DropDownList dropRight = (DropDownList)e.Row.FindControl("dropRightList");
                if (dropRight != null)
                {
                    dropRight.DataSource = GetRightList();
                    dropRight.DataTextField = "RName";
                    dropRight.DataValueField = "ID";
                    dropRight.DataBind();
                    dropRight.SelectedValue = hidRight.Value;
                    hidRightID.Value = hidRight.Value;
                }
            }
        }
    }
}
